var map, drawController;
var vectorLayer;
var WGS_PROJECTION = new OpenLayers.Projection("EPSG:4326");
var SPHERICAL_MERCATOR_PROJECTION = new OpenLayers.Projection("EPSG:900913");
var styleMap;
var singleSelectControl, multipleSelectControl;
var rules = new Array();
var tolerance = 400;

function init() {
    
	var defaultStyle = new OpenLayers.Style({
        label: '${num_points}',
        fontColor: '#fff',
        fontFamily: 'arial, sans-serif',
        fontSize: '.9em',
        fontWeight: 'bold',
        graphicZIndex: 1,
        pointRadius: '${point_radius}', 
        strokeColor: '#fff',
		strokeWidth: 2.5,
        fillColor: '${color}', 
        fillOpacity: '${severity}'
    },{
        context: {
            num_points: function(feature){
                if(feature.attributes.count > 1)
                    return feature.attributes.count;
                else{
                    return '';
                }
            },
            point_radius: function(feature){ 
                if(feature.attributes.count > 1)
                    return 10*(feature.attributes.count);
                else{
                    return 10;
                }
            } 
        }
    });

	var selectStyle = new OpenLayers.Style({
		'strokeColor': '#1307f1'
	});

	styleMap = new OpenLayers.StyleMap({
		'default': defaultStyle,
		'select': selectStyle
	});
	
	vectorLayer = new Array();

	//MAP
	map = new OpenLayers.Map("map", {
		displayProjection: WGS_PROJECTION,
		restrictedExtent: new OpenLayers.Bounds(116.27, 4.39, 127, 21.53).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION)
	});
    var osm = new OpenLayers.Layer.Google('Google Map Layer', {numZoomLevels: 20});

	map.addLayers([osm]);
   	map.addControl(new OpenLayers.Control.MousePosition());

	map.setCenter(new OpenLayers.LonLat(121.06850, 14.64866).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION), 15);
}

function defineFeature(feature) {
	if( feature.cluster ){
        total = 0;
        size = feature.cluster.length;
        for(i=0; i<size; i++) {
          total += parseInt(feature.cluster[i].attributes.index);
        }
        feature.attributes.index = parseInt(total/size);
        feature.attributes.description = size + " reports in this Cluster";
        feature.attributes.color = feature.cluster[size-1].attributes.color;
    }
}

//detail.html
function setMapCenter(lon, lat){
	 map.setCenter(new OpenLayers.LonLat(lon, lat).transform(WGS_PROJECTION, SPHERICAL_MERCATOR_PROJECTION), 17);
}
